﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
using DevExpress.XtraGrid;
using DevEx;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Xml;
using DevExpress.XtraGrid;
using DevExpress.Xpf;
using DevExpress.Utils;
using DevEx.QuocSy.UserControl;

namespace DevEx
{
    public partial class frmHangTon : DevExpress.XtraEditors.XtraForm
    {
        private int _ID_Kho;
        QLVTDataContext dc = new QLVTDataContext();
        public XtraUserControl _uc;

        
        //public XtraUserControl uc
        //{
        //    get { return _uc; }
        //    set { _uc = uc; }
        //}

        public frmHangTon(int ID_Kho, XtraUserControl uc)
        {
            InitializeComponent();
            _ID_Kho = ID_Kho;
            KhoiTaodtgTon(ID_Kho);
            _uc = uc;
        }

        private void frmHangTon_Load(object sender, EventArgs e)
        {
            txtSLXuat.Visible = false;
            gridView1.OptionsBehavior.Editable = false;
            //set editable = false để bắt event double click
        }

        public void KhoiTaodtgTon(int ID_Kho)
        {
            var q = from p in dc.tblTons
                    where p.ID_Kho == ID_Kho && p.SLTonTH > 0
                    orderby p.tblVatTu.VatTu, p.Code
                    select new
                    {
                        p.ID_MatHang,     
                        p.SoTheKho,
                        p.Code,
                        p.tblNguonVatTu.KH_NguonVatTu,
                        p.tblHopDong.SoHopDong,
                        p.tblVatTu.VatTu,
                        p.tblVatTu.MaKyHieu,
                        p.tblCCL.CCL,
                        p.NamSX,
                        p.Gia,                        
                        p.SLTonKH,
                        p.SLTonTH
                    };

            dtgTon.DataSource = q;
            FormatdtgPhieuNhap();
        }

        public void FormatdtgPhieuNhap()
        {
            gridView1.Columns.ColumnByFieldName("ID_MatHang").Visible = false;
            ///gridView1.Columns.ColumnByFieldName("ID_NguonVatTu").Visible = false;
                 
            //Columns VatTu       
            var cl_VatTu = gridView1.Columns.ColumnByFieldName("VatTu");
            cl_VatTu.Width = 200;
            cl_VatTu.Caption = "Vật Tư";
            cl_VatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns MaKyHieu
            var cl_MaKyHieu = gridView1.Columns.ColumnByFieldName("MaKyHieu");
            cl_MaKyHieu.Width = 150;
            cl_MaKyHieu.Caption = "Mã Ký hiệu";
            cl_MaKyHieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_MaKyHieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns SoTheKho       
            var cl_SoTheKho = gridView1.Columns.ColumnByFieldName("SoTheKho");
            cl_SoTheKho.Width = 60;
            cl_SoTheKho.Caption = "Thẻ kho";
            cl_SoTheKho.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SoTheKho.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Code       
            var cl_Code = gridView1.Columns.ColumnByFieldName("Code");
            cl_Code.Width = 30;
            cl_Code.Caption = "Code";
            cl_Code.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Code.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns CCL
            var cl_CCL = gridView1.Columns.ColumnByFieldName("CCL");
            cl_CCL.Width = 50;
            cl_CCL.Caption = "CCL";
            cl_CCL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_CCL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Nguon Tai Chinh
            var cl_NguonVatTu = gridView1.Columns.ColumnByFieldName("KH_NguonVatTu");
            cl_NguonVatTu.Width = 30;
            cl_NguonVatTu.Caption = "N. TC";
            cl_NguonVatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NguonVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Nguon Nhap
            var cl_NguonNhap = gridView1.Columns.ColumnByFieldName("SoHopDong");
            cl_NguonNhap.Width = 60;
            cl_NguonNhap.Caption = "Nguồn gốc";
            cl_NguonNhap.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NguonNhap.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;           

            //Columns NamSX
            var cl_NamSX = gridView1.Columns.ColumnByFieldName("NamSX");
            cl_NamSX.Width = 50;
            cl_NamSX.Caption = "Năm SX";
            cl_NamSX.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NamSX.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            ////Columns MaKyHieu
            //var cl_MaKyHieu = gridView1.Columns.ColumnByFieldName("MaKyHieu");
            //cl_MaKyHieu.Caption = "Mã Hiệu";
            //cl_MaKyHieu.Width = 70;
            //cl_MaKyHieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            //cl_MaKyHieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;                     
          
            //Columns Gia
            var cl_Gia = gridView1.Columns.ColumnByFieldName("Gia");
            cl_Gia.Width = 70;
            cl_Gia.Caption = "Đơn giá";
            cl_Gia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_Gia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Gia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_Gia.DisplayFormat.FormatString = "n0";            
                      
            //Columns SLTonKH
            var cl_SLTonKH = gridView1.Columns.ColumnByFieldName("SLTonKH");
            cl_SLTonKH.Width = 60;
            cl_SLTonKH.Caption = "SL Tồn KH";
            cl_SLTonKH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SLTonKH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SLTonKH.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SLTonKH.DisplayFormat.FormatString = "#,###.##";

            //Columns SLTonTH
            var cl_SLTonTH = gridView1.Columns.ColumnByFieldName("SLTonTH");
            cl_SLTonTH.Width = 60;
            cl_SLTonTH.Caption = "SL Tồn TH";
            cl_SLTonTH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SLTonTH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SLTonTH.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SLTonTH.DisplayFormat.FormatString = "#,###.##";
        }
       
        private void txtSLXuat_KeyDown(object sender, KeyEventArgs e)
        {
            //kiểm tra số lượng xuất < slton thực tế hay không
            
            if (e.KeyCode == Keys.Enter)
            {
                double SLTonTH = Convert.ToDouble(gridView1.GetFocusedRowCellValue("SLTonTH"));
                double SLTonKH = Convert.ToDouble(gridView1.GetFocusedRowCellValue("SLTonKH"));
                double SLXuat = Convert.ToDouble(txtSLXuat.EditValue);
                if ((SLXuat > SLTonTH) || (SLXuat > SLTonKH))
                {
                    MessageBox.Show("Số lượng xuất phải nhỏ hơn số lượng tồn theo kế hoạch và trong kho.");
                    return;
                }
                int ID_MatHang = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_MatHang"));
                if (_uc.Name == "ucChuyenKhoNoiBo")
                {
                    ((ucChuyenKhoNoiBo)_uc).CreatNewRowTon(ID_MatHang, Convert.ToDouble(txtSLXuat.EditValue));
                }
                else if (_uc.Name == "ucPhieuXuat")
                {
                    ((ucPhieuXuat)_uc).CreatNewRowTon(ID_MatHang, Convert.ToDouble(txtSLXuat.EditValue));
                }
                                
                txtSLXuat.EditValue = "";
                txtSLXuat.Visible = false;
                lblSLXuat.Visible = false;
                lblSLXuat.Text = "/";
            }
        }

        private void dtgTon_DoubleClick(object sender, EventArgs e)
        {
            txtSLXuat.Visible = true;
            lblSLXuat.Visible = true;
            Double min;
            Double SLKH =Convert.ToDouble(gridView1.GetFocusedRowCellValue("SLTonKH"));
            Double SLTH =Convert.ToDouble(gridView1.GetFocusedRowCellValue("SLTonTH"));

            if (SLKH > SLTH)
                min = SLTH;
            else
                min = SLKH;

            lblSLXuat.Text = "/" + min.ToString("#,###.##");
            txtSLXuat.Focus();
        }
    }
}